<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <style type="text/css">
        body {
            font-family:Arial,Helvetica,Sans Serif;
            font-size:10pt;
        }
    </style>
    <link rel="stylesheet" href="./theme/default/style.css" type="text/css" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <script type="text/javascript"  src="./OpenLayers.js"></script>
   <script type="text/javascript" src="./json2.js"></script>
   <script type="text/javascript" src="./lib/proj4js-combined.js"></script>
   <script type="text/javascript" src="./lib/defs/EPSG4326.js"></script>
   <script type="text/javascript" src="./lib/defs/EPSG900913.js"></script>
   <script type="text/javascript" src="./lib/defs/EPSG2193.js"></script>
   <script type="text/javascript" src="./lib/defs/EPSG27200.js"></script>
   <script type="text/javascript">
       OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
                defaultHandlerOptions: {
                    'single': true,
                    'double': false,
                    'pixelTolerance': 0,
                    'stopSingle': false,
                    'stopDouble': false
                },

                initialize: function(options) {
                    this.handlerOptions = OpenLayers.Util.extend(
                        {}, this.defaultHandlerOptions
                    );
                    OpenLayers.Control.prototype.initialize.apply(
                        this, arguments
                    );
                    this.handler = new OpenLayers.Handler.Click(
                        this, {
                            'click': this.trigger
                        }, this.handlerOptions
                    );
                },

                trigger: function(e) {
                    var lonlat = map.getLonLatFromViewPortPx(e.xy);
                    //alert("You clicked near " + lonlat.lat + " N, " +
                    //                          + lonlat.lon + " E");
                    document.getElementById('srid').value="NZTM";
                    var script = document.createElement('script');
                            document.body.appendChild(script);
                        //script.src = 'http://localhost:8084/soilscalefactor/resources/luclookup/?lat=' +
                        script.src = 'http://roj.zen.landcareresearch.co.nz:8080/soilscalefactor/resources/luclookup/?lat=' +
                           // lonlat.lat + '&lon=' + lonlat.lon + '&srid=' + 900913 + '&callback=loadJSON';
                            lonlat.lat + '&lon=' + lonlat.lon + '&srid=' + 2193 + '&callback=loadJSON';
                }

            });

    //function GetLUC(){
    //    var script = document.createElement('script');
    //    document.body.appendChild(script);
    //    var srid = 0;
    //    switch(document.getElementById('srid').value){
    //        case "NZTM": srid=2193;break;
    //        case "NZMG": srid=27200;break;
    //        case "WGS84": srid=4326;
    //    }
    //	script.src = 'http://localhost:8084/soilscalefactor/resources/luclookup/?lat='
    //    //script.src = 'http://arwen:8080/soilscalefactor/resources/luclookup/?lat='
    //        + document.getElementById('lat').value
    //        + '&lon=' + document.getElementById('lon').value
    //        + '&srid=' + srid + '&callback=loadJSON'
    //    //alert("script: " + script.src.toString());
    //}

    function loadJSON(result) {
	strResult = JSON.stringify(result);
        //alert(strResult);
        //document.getElementById("result").value = strResult;
        var obj = JSON.parse(strResult);
        if (obj.result != null) //if there is stuff in the result ...
                //var source = new Proj4js.Proj('EPSG:900913');
                    var source = new Proj4js.Proj('EPSG:2193');
                var dest = new Proj4js.Proj('EPSG:2193');
                var p = new Proj4js.Point(obj.result.lon+','+obj.result.lat);
                Proj4js.transform(source, dest, p);
		{if (obj.result.scalefactor != null)
			{
			drain_clas = obj.result.drain_clas;
                        praw_class = obj.result.praw_class;
                        scalefactor =  obj.result.scalefactor.split(',');
                        lat =  p.y;
                        lon =  p.x;
                        }
                }
         document.getElementById("drain_clas").value = drain_clas;
         document.getElementById("praw_class").value = praw_class;
         document.getElementById("scalefactor2009").value = scalefactor[0];
         document.getElementById("scalefactor2010").value = scalefactor[1];
         document.getElementById("scalefactor2011").value = scalefactor[2];
         document.getElementById("lat").value = lat;
         document.getElementById("lon").value = lon;
    }



    function load() {
      OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
      var mapOptions = { 	projection: new OpenLayers.Projection("EPSG:2193"),
				units: 'm',
				maxExtent: new OpenLayers.Bounds(1000000,4700000,2200000,6300000),
				numZoomLevels: 10,
				scales: [10000, 25000, 50000, 100000, 250000, 500000, 1000000, 2000000, 4000000, 8000000]};

      map = new OpenLayers.Map('map',mapOptions);

      var bmap = new OpenLayers.Layer.WMS(
            "Basemap",
            "http://maps.scinfo.org.nz/tilecache/wms-c/tilecache.py?",
            {
                layers: "lcr_basemap_cache_nztm",
                transparent: "true",
                format: "image/png",
                projection: "EPSG:2193"
            },
            {isBaseLayer: true}
        );

	// Create the science layer, note opacity is switched on
        var lri_luc = new OpenLayers.Layer.WMS(
            "Land Use Capability",
            "http://maps.scinfo.org.nz/tilecache/wms-c/tilecache.py?",
            {
                layers: "lri_luc_main_cache_nztm",
                transparent: "true",
                format: "image/png"
            },
            {isBaseLayer: false, opacity: 0.5}
        );
        var copyright = new OpenLayers.Layer.WMS(
            "Copyright",
            "http://maps.scinfo.org.nz/basemaps/wms?",
            {
                layers: "copyright",
                transparent: "true",
                format: "image/png"
            },
            {isBaseLayer: false, singleTile: true, ratio: 1, displayInLayerSwitcher: false}
        );
        var nzfsl = new OpenLayers.Layer.WMS( "Fundamental Soil Layers",
                    "http://roj.zen.landcareresearch.co.nz:8080/geoserver/wms",
			{
				layers: 'lcr:fsl-north-island-all-attributes',
				format: 'image/png',
                                transparent: "true"
			}, {transitionEffect: 'resize', opacity: 0.7, visibility: false}
		);

        map.addLayers([bmap,lri_luc,nzfsl,copyright]);
        map.addControl(new OpenLayers.Control.Scale());
	map.addControl(new OpenLayers.Control.LayerSwitcher());

	// set the center of the map and the zoom scale to zoom to
        map.setCenter(new OpenLayers.LonLat(1600000,5500000), 0 );

    //map =  new OpenLayers.Map( "map", {
    //    projection: new OpenLayers.Projection("EPSG:900913"),
    //    //displayProjection: new OpenLayers.Projection("EPSG:4326"),
    //    units: "m",
    //    numZoomLevels: 20,
    //    maxResolution: 156543.0339,
    //    maxExtent: new OpenLayers.Bounds( - 20037508, -20037508, 20037508, 20037508.34)
    //});
     
     //map.addControl( new OpenLayers.Control.LayerSwitcher() );

    //var nzfsl = new OpenLayers.Layer.WMS( "Fundamental Soil Layers",
    //                "http://roj.zen.landcareresearch.co.nz:8080/geoserver/wms",
//		{
//				layers: 'lcr:fsl-north-island-all-attributes',
//				format: 'image/png',
//                                transparent: "true"
//			}, {transitionEffect: 'resize', opacity: 0.7, visibility: false}
//		);
//
//    var osm = new OpenLayers.Layer.OSM();
//
//     map.addLayers([nzfsl,osm]);

//     var bounds = new OpenLayers.Bounds(19904000, -5991200, 18623750, -4069156); //TODO - bind this to maximum layer extent
//     map.zoomToExtent(bounds);

     var click = new OpenLayers.Control.Click();
                map.addControl(click);
                click.activate();
    }
   
</script>


        <!--<script src="gmap-wms-alt.js" type="text/javascript"></script>-->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Soil Scale Factor</title>
    </head>
    <body onload="load()">
    <h1>Soil Scale Factor</h1>
    <b>lon</b> <input type="text" id="lon"/><b>lat</b> <input id="lat" type="text"/> <select id="srid">
<option>NZTM</option>
<option>NZMG</option>
<option>WGS84</option>
</select>
 <!--<input type="submit" name="getluc" onclick="GetLUC()"/>-->
    <br /><br/>
    <!--<b>result</b> <input type="text" size="100"  id="result"/>
    <br/><br/>-->
    <b>drain class</b> <input type="text" size="15"  id="drain_clas"/>
    <b>praw class</b> <input type="text" size="15"  id="praw_class"/>
    <br/><b>Scale Factor:</b><br/>
    <b> 2009</b> <input type="text" size="15"  id="scalefactor2009"/>
    <b>2010</b> <input type="text" size="15"  id="scalefactor2010"/>
    <b>2011</b> <input type="text" size="15"  id="scalefactor2011"/>
    <br /><br/>
    tip: zoom in with shift-left click 'n drag, pan with left click 'n drag, query with left click
    <br/><br/>
    <div id="map" style="width: 750px; height: 600px"></div>
    <div id="paneldiv" class="olControlNavToolbar"></div>
    </body>
</html>
